<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="头像" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="avatar">
              <img :src="avatarUrl" style="width:100px;height: 100px">
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="手机号码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input :maxLength="11" v-model="model.phone" placeholder="请输入手机号码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="昵称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nickname">
              <a-input v-model="model.nickname" placeholder="请输入昵称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sex">
<!--              <a-input v-model="model.sex" placeholder="请输入性别"  ></a-input>-->
              <j-dict-select-tag v-model="model.sex" @change="sexChange" dictCode="sex" placeholder="请选择性别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="生日" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="birthday">
              <j-date placeholder="请选择生日"  v-model="model.birthday" date-format="YYYY-MM-DD" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="居住地" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="liveAddress">
              <j-area-linkage @getText="getAddressText" v-model="model.liveAddress" type="cascader"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="邀请码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="invitationCode">
              <a-input v-model="model.invitationCode" placeholder="请输入邀请码"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { duplicateCheck } from '@/api/api'

  export default {
    name: 'AppUserForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          phone: [
            { required: true, message: '请输入手机号码!'},
            {validator: this.validatePhone}
          ],
          nickname: [
            { required: true, message: '请输入昵称!'},
          ],
          sex: [
            { required: true, message: '请选择性别!'},
          ],
          birthday: [
            { required: true, message: '请选择生日!'},
          ],
          liveAddress: [
            { required: true, message: '请选择居住地!'},
          ],
        },
        url: {
          add: "/app/xyAppUser/register",
        },
        avatarUrl:require('@/assets/xy_logo.png'),
        addressText:"",
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },


      //获取头像
      sexChange(sex){
        getAction("/user/appUser/getAvatarUrl", {sex:sex}).then(res => {
          if (res.success) {
            this.avatarUrl=res.result;
          } else {
            this.$message.warn(res.message)
          }
        })
      },

      //验证手机号码
      validatePhone(rule, value, callback){
        if(!value){
          callback()
        }else{
          if(new RegExp(/^1[3|4|5|6|7|8|9][0-9]\d{8}$/).test(value)){
            var params = {
              tableName: 'xy_app_user',
              fieldName: 'phone',
              fieldVal: value,
              dataId: this.userId
            };
            duplicateCheck(params).then((res) => {
              if (res.success) {
                callback()
              } else {
                callback("手机号已存在!")
              }
            })
          }else{
            callback("请输入正确格式的手机号码!");
          }
        }
      },
      //获取地址的中文
      getAddressText(text){
        this.addressText=text;
      },

      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            if(this.addressText){
              this.model.liveAddress=this.addressText;
            }else{
              this.$message.warning("请选择居住地");
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>